package tech.aistar.controller.noteshow;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.config.GuidelInterceptor;
import tech.aistar.entity.authordis.NoteAuthor;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.entity.daoyou.GuideCodeStatus;
import tech.aistar.entity.daoyou.GuideResult;
import tech.aistar.service.editnotes.IEditNotesService;
import tech.aistar.util.date_SimpleDateFormat.DateUtileDemo;
import tech.aistar.util.editnotes.CheckWords;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Set;

/**
 * Created by Dell on 2019/9/26.
 */
@Controller
public class EditNotesController {


    //@Autowired
   // private NotesMapper notesMapper;
//    @Autowired
//    private OrderMapper orderMapper;


    private CheckWords checkWords;

    @Autowired
    private IEditNotesService editNotesService;



    @RequestMapping("/checkword")
    @ResponseBody
    public GuideResult checkNotesWord(String checktext, Model model){
        System.out.println(checktext);
        Set<Object> map=null;
        try {
             map = checkWords.checkText(checktext);

        } catch (Exception e) {
            e.printStackTrace();
        }
        if(map.size()== 0){
            return new GuideResult(GuideCodeStatus.OK,true,"合格");
        }else{
            model.addAttribute("map",map);
            return new GuideResult(GuideCodeStatus.OK,false,"不合格",map);
        }

    }

    /**
     * @Description:跳转写游记页面
     * 查询游记作者
     * 配置拦截器
     * @param:
     * @author XiaoDi
     * @date 2019/10/6 15:22
    */
    //拦截器
    @GuidelInterceptor
    @RequestMapping("/editnote")
    public String editNotes(Model model, HttpServletRequest request){

        //登录之后,拿到userTable
        HttpSession session=request.getSession();
        UserTable userTable= (UserTable) session.getAttribute("userId");

        NoteAuthor noteAuthor = editNotesService.findNoteAuthor(userTable.getId());

        model.addAttribute("noteAuthor",noteAuthor);

        return "traveler_show/editnotes";
    }

    /**
     * @Description:保存游记
     * @param:
     * @author XiaoDi
     * @date 2019/10/6 15:22
    */
    @RequestMapping("/savenotes")
    @ResponseBody
    public GuideResult saveEditNotes(Integer autuor_id,Integer note_type,String content,String content2,String content3,String content4,String photo,String photo2,String photo3,String photo4,String title,String publish_time,Model model){

        Date now = new Date();
        String time = DateUtileDemo.format("yyyy-MM-dd HH:mm:ss",now);

        //向文章表中插入数据
        editNotesService.saveNotes(autuor_id,title,note_type,time,photo);
        //查找文章id
        Integer notes_id = editNotesService.findNotesId(title,time);
        //向文章详情表中插入内容
        //插入第一段
        editNotesService.saveNotesInfo(notes_id,1,null,content);


        if(notes_id == null){
            return new GuideResult(GuideCodeStatus.OK,false,"不成功");
        }else{
            model.addAttribute("notes_id",notes_id);
            return new GuideResult(GuideCodeStatus.OK,true,"成功",notes_id);
        }
    }


    @RequestMapping("/success")
    public String editNotesSuccess(){
        return "traveler_show/editnotes_success";
    }

}
